<?php
/**
 * 关注相关
 *
 */
class attention_model_attention extends Model{
	
	public function __construct()
	{
		parent::__construct();
		$this->table 		 = 'cartoon_attention';			//关注关系表
	}
	
	/**
	 * 添加关注
	 *
	 * @param  int $uid
	 * @param int $cuid
	 */
	public function add($uid,$cuid){
		if(!($uid && $cuid)) return false;
		$args['uid'] 	= 	$uid;
		$args['c_uid']	=	$cuid;
		$args['addtime']=	CURRENT_TIME;
		if($this->has($uid,$cuid)) return ERR_PARAMATER;
		return $this->db->insert($this->table,$args);
	}
	/**
	 * 取消关注
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $cuid
	 * @return unknown
	 */
	public function cancel($uid,$cuid){
		if(!($uid && $cuid)) return false;
		return $this->db->query("DELETE FROM $this->table WHERE uid=$uid AND c_uid = $cuid");
	}
	
	/**
	 * 判断是否为关注的人
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $cuid
	 */
	public function has($uid,$cuid){
		$sql = "SELECT id FROM $this->table WHERE uid=$uid AND c_uid=$cuid";
		return $this->db->fetchField($sql,'id');
	}
	
	/**
	 * 获取我关注的列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getListPage($uid,$page=1,$pageSize=16){
		$sql = "SELECT c_uid AS uid FROM $this->table WHERE uid=$uid ORDER BY id DESC" ;
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 获取我关注的最新$num个关注列表。如果$num 为0，在表示所有
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $num
	 * @return unknown
	 */
	public function getList($uid,$num=0){
		$sql = "SELECT c_uid AS uid FROM $this->table WHERE uid=$uid ORDER BY id DESC" ;
		$num>0 && $sql.=" LIMIT $num";
		return $this->db->fetchAll($sql);
	}
	
	
	/**
	 * 获取关注我的列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getMyListPage($uid,$page=1,$pageSize=16){
		$sql = "SELECT uid AS uid FROM $this->table WHERE c_uid=$uid ORDER BY id DESC" ;
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 获取关注我的最新$num个关注列表。如果$num 为0，在表示所有
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $num
	 * @return unknown
	 */
	public function getMyList($uid,$num=0){
		$sql = "SELECT uid AS uid FROM $this->table WHERE c_uid=$uid ORDER BY id DESC" ;
		$num>0 && $sql.=" LIMIT $num";
		return $this->db->fetchAll($sql);
	}
	
	
}